package com.springboot.eurekacosumerdemo.controller;

import com.springboot.eurekacosumerdemo.config.RemoteService;
import com.springboot.eurekacosumerdemo.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class GoodsController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RemoteService remoteService;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/getGoods")
    public Goods getGoods() {
        String url = "http://localhost:8000/getGoods";
        Goods goods = restTemplate.getForObject(url, Goods.class);
        return goods;
    }

    @GetMapping("/getGoods3")
    public Goods getGoods3() {
        ServiceInstance choose = loadBalancerClient.choose("eureka-provider");
        String url = String.format("http://%s:%s/getGoods",choose.getHost(),choose.getPort());
        Goods goods = restTemplate.getForObject(url, Goods.class);
        return goods;
    }

    @GetMapping("/getGoods4")
    public Goods getGoods4() {
        String url = String.format("http://%s/getGoods","eureka-provider");
        Goods goods = restTemplate.getForObject(url, Goods.class);
        return goods;
    }

    @GetMapping("/getGoods2")
    public Goods getGoods2() {
        return remoteService.getGoods();
    }
}
